const router = require('koa-router')()
const db = require('../db/initdb')
const fs = require('fs');
const path = require('path');
const { koaBody } = require('koa-body')


router.prefix('/material')

// 拿数据
router.get('/msg', async (ctx, next) => {
  // console.log(ctx.query);
  let type = ctx.query.type;
  let currentPage = ctx.query.currentPage;
  let pageSize = ctx.query.pageSize;
  let sql = "select * from hs.card where type=$1 order by voicenum desc, date desc"
  let result = await db.query(sql, [type]);
  let pageList = [];
  for (let i = (currentPage - 1) * pageSize; i < result.rows.length; i++) {
    pageList.push(result.rows[i]);

    if (pageList.length == pageSize)
      break;
  }
  // console.log(pageList);
  ctx.body = {
    pageList,
    total: result.rows.length
  };
})

// 图片
router.get('/public/images/:name', async (ctx, next) => {
  // console.log("name:" + ctx.params.name);
  let filepath = path.join(__dirname, "../public/images/" + ctx.params.name)
  let file = fs.readFileSync(filepath)
  ctx.set('Content-Type', "Access-Control-Allow-Origin:'http://localhost:1234'")
  ctx.body = file;
})
// 视频
router.get('/public/video/:name', async (ctx, next) => {
  // console.log(ctx.params.name);
  let filepath = path.join(__dirname, "../public/images/" + ctx.params.name)
  let file = fs.readFileSync(filepath)
  // ctx.set('Content-Type', "Access-Control-Allow-Origin:'http://localhost:1234'")
  ctx.body = file
})
// 编辑
router.post('/edit', async (ctx, next) => {
  // console.log(ctx.request.body);
  let values = ctx.request.body;
  let sql = 'UPDATE hs.card set name=$1,outline=$2,voicenum=$3,starnum=$4 where id=$5 returning *';
  let result = await db.query(sql, [values.name, values.outline, values.voicenum, values.starnum, values.id]);
  // console.log(result.rows[0]);
  ctx.body = result.rows[0];
})

router.post('/edit/image', koaBody(), async (ctx, next) => {
  // console.log(ctx.query.id);
  let id = ctx.query.id;
  let filename = (ctx.request.files.file.newFilename);
  let sql = 'UPDATE hs.card set perface=$1 where id=$2 returning perface';
  let result = await db.query(sql, [filename, id]);
  // console.log(result.rows[0]);
  ctx.body = result.rows[0];

})
// 添加
router.post('/add', async (ctx, next) => {
  console.log(ctx.request.body);
  let values = ctx.request.body;

  let sql = 'insert into hs.card(name,type,outline,userid,voicenum,starnum,image,perface,date) values ($1,$2,$3,$4,$5,$6,$7,$8,$9) returning *';
  let result = await db.query(sql, [values.name, values.type, values.outline, values.userid, values.voicenum, values.starnum, values.image, values.perface, Date.now()]);
  // console.log(result.rows[0])
  ctx.body = result.rows[0];
})

router.post('/add/image', koaBody(), async (ctx, next) => {
  let filename = (ctx.request.files.file.newFilename);

  ctx.body = filename;
})

router.post('/add/video', koaBody(), async (ctx, next) => {
  let filename = (ctx.request.files.file.newFilename);
  ctx.body = filename;
})

// 删除
router.post('/del', async (ctx, next) => {
  console.log(ctx.request.body);
  let id = ctx.request.body.id;
  let sql = "delete from hs.card where id=$1";
  let result = await db.query(sql, [id]);
  ctx.body = "删除成功！";
})

// 查找
router.get('/search', async (ctx, next) => {
  console.log(ctx.query);
  let value = ctx.query.value;
  let keySearch = value.trim();
  let type = ctx.query.type;
  let currentPage = ctx.query.currentPage;
  let pageSize = ctx.query.pageSize;

  let sql = "select * from hs.card where type=$1";
  let result = await db.query(sql, [type]);
  // console.log(result.rows);
  let list = result.rows.filter(item => {
    if (item.name.includes(keySearch)) {
      return item
    } else if (item.outline.includes(keySearch)) {
      return item
    } else if (item.voicenum == keySearch) {
      return item
    } else if (item.starnum == keySearch) {
      return item
    }
  })
  let searchList = []
  for (let i = (currentPage - 1) * pageSize; i < list.length; i++) {
    searchList.push(list[i]);
    if (searchList.length == pageSize)
      break;
  }
  ctx.body = {
    searchList,
    total: list.length
  };
})
module.exports = router
